package com.widevision.adapter;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import net.widevision.android.R;
import net.widevision.android.TestPost;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;



import com.widevision.ListActivity.UserProfile;
import com.widevision.utility.ImageLoader;

public class TweetAdapter extends BaseAdapter
{
    public Activity context;
    public LayoutInflater inflater;
    static int h=0;
    
    InputStream is;
    ImageLoader imageLoader;
    
    
    ArrayList<HashMap<String, String>> data;
	
    public TweetAdapter(Activity context,ArrayList<HashMap<String, String>> d)
    {
        super();
        this.context = context;
        this.data = d;
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    public int getCount()
    {
    	return data.size();
    }
   
      
      public Object getItem(int position) 
      {
      	 return position;
      }
   
    
      public long getItemId(int position)
      {
      	return position;
      }
      
 
     public static class ViewHolder
    {
        ImageView profileimg,minitwimg,timeimg,imageview;
        TextView screenName,userName,tweet,time,timeago;
        LinearLayout optionlayout,tweetlayout;
    }
 
 
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
      
    	final ViewHolder holder= new ViewHolder();
        if(convertView==null)
                
        	convertView = inflater.inflate(R.layout.tweetitemlist, null);
       		holder.imageview  = (ImageView) convertView.findViewById(R.id.tw_replyzzzz);
            holder.profileimg  = (ImageView) convertView.findViewById(R.id.profileimg);
            holder.minitwimg   = (ImageView) convertView.findViewById(R.id.timeimg);
            holder.timeimg     = (ImageView) convertView.findViewById(R.id.minitwimg);
            holder.screenName  = (TextView) convertView.findViewById(R.id.screenName);
            holder.tweet       = (TextView) convertView.findViewById(R.id.tweettext);
            holder.userName    = (TextView) convertView.findViewById(R.id.username);
            holder.time        = (TextView) convertView.findViewById(R.id.time);
            
            holder.optionlayout=(LinearLayout) convertView.findViewById(R.id.optionLayout);

         holder.tweetlayout =(LinearLayout) convertView.findViewById(R.id.tweetoption);
            
            convertView.setTag(holder);
            
       
            HashMap<String, String> tweetmap = new HashMap<String, String>();
            tweetmap = data.get(position);
            
            //SETTING ALL ITEM VALUES
       
	        holder.userName.setText(tweetmap.get(UserProfile.USER_NAME));
	        holder.screenName.setText(tweetmap.get(UserProfile.SCREEN_NAME));
	        holder.time.setText(tweetmap.get(UserProfile.TIME));
	        holder.tweet.setText(tweetmap.get(UserProfile.TWEET));
	        
	        String u=tweetmap.get(UserProfile.USER_THUMB_URL);
	        System.out.println("u="+u);
	        URL url = null;
			try
			{
				url = new URL(u);
			} 
			catch (MalformedURLException e1)
			{
				e1.printStackTrace();
			}
	        
	        try
			{
				is =url.openStream();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			Drawable twetimgobj = Drawable.createFromStream(is, "src name");
			holder.profileimg.setImageDrawable(twetimgobj);
	        
//			if(selectedPos == position)
//	        {
//	        	if(h==0)
//        		{
//	        		holder.optionlayout.setVisibility(View.VISIBLE);
//	        		Log.d("if>>>>>>>>", ""+h);
//	        		h=1;
//        		}
//	        	else if(h==1)
//	        	{
//	        		holder.optionlayout.setVisibility(View.GONE);
//	        		Log.d("else>>>>>>>>", ""+h);
//	        		h=0;
//	        	}
//	        	Log.d("position>>>>>>>>", ""+position);
//            }
//	        else
//	        {
//	        	holder.optionlayout.setVisibility(View.GONE);
//	        	Log.d("position>>>>>>>>", ""+position);
//	        }
			
			convertView.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					//holder.optionlayout.setVisibility(View.VISIBLE);
					
					if(holder.optionlayout.getVisibility()==View.VISIBLE)
					{
						holder.optionlayout.setVisibility(View.GONE);
					}
					else if(holder.optionlayout.getVisibility()==View.GONE)
					{
						holder.optionlayout.setVisibility(View.VISIBLE);
					}
					
				}
			});
			
//			holder.imageview.setOnClickListener(new OnClickListener(){
//				
//				@Override
//				public void onClick(View v)
//				{       
//					
//				   Intent i=new Intent(context,TestPost.class);
//				      i.putExtra("SCREENNAME", StrScreeName);
//				   
//				       
//				     context.startActivity(i);
//				     
//				   
//					
//				}
//
//			
//			});
//			holder.imageview.setOnClickListener(new OnClickListener() {
//			    
//			    
//			    public void onClick(View v) 
//			    {
//			     
//			     Intent i=new Intent(context,TestPost.class);
////			     String srcname=(String) holder.screenName.getText();
//////			     int userId=Integer.parseInt(srcname);
////			     i.putExtra("SCREENNAME", srcname);
//			     context.startActivity(i);
//			    }
//			    });
			        
			         convertView.setTag(holder);
			         return convertView;
	        
	        
    }

	
    
}      
	  
        
       
        
  
 


